package com.overseasShopping.Action;

import java.io.PrintWriter; 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.Biz.WalletBiz;
import com.overseasShopping.Biz.WalletDetailBiz;
import com.overseasShopping.pojo.User;
import com.overseasShopping.pojo.Wallet;
import com.overseasShopping.pojo.WalletDetail;
import com.overseasShopping.util.ListUtil;
 
public class WalletAction extends ActionSupport{
	private String userId;
	private String id;
	private String str;
	private String sum;
	private String getUserId;
	private String password;
	private WalletBiz walletBiz;
	private WalletDetailBiz walletDetailBiz;
	private float sum2;
	
	public void setUserId(String userId) {
		this.userId = userId;
	}



	public float getSum2() {
		return sum2;
	}

	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	public void setSum2(float sum2) {
		this.sum2 = sum2;
	}



	public String getGetUserId() {
		return getUserId;
	}



	public void setGetUserId(String getUserId) {
		this.getUserId = getUserId;
	}



	public String getSum() {
		return sum;
	}



	public String getUserId() {
		return userId;
	}



	public String getId() {
		return id;
	}



	public void setId(String id) {
		this.id = id;
	}



	public void setStr(String str) {
		this.str = str;
	}



	public void setSum(String sum) {
		this.sum = sum;
	}



	public String showBalance() throws Exception {
		Float balance= walletBiz.getBalance(userId);
		Map<String, Object> map=ActionContext.getContext().getSession();
		map.put("balance", balance);
		HttpServletResponse response=ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		out.print(balance);
		out.flush();
		out.close();
		return SUCCESS;
	}
	
	public String showWalletDetail() throws Exception{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		Wallet wallet=walletBiz.getWallet(userId);
		System.out.println(wallet);
		if(wallet.getWalletDetail()!=null){
			List<WalletDetail> walletDetails=ListUtil.getInstance().setToList(wallet.getWalletDetail());
			if(walletDetails.size()>0){
				out.print(walletDetails);
			}else{
				out.print("没有任何记录！");
			}
		}else{
			out.print("没有任何记录！");
		}
		out.flush();
		out.close();
		return SUCCESS;
	}
	
	public String deleteWallet() throws Exception{
		//解析要删除的自增列id
		String[] idsString1=str.split("_ ");
		String[] id;
		if(idsString1.length>1){
			char character=str.charAt(1);
			if(character==' '){
				id=new String[idsString1.length-1];
				for(int i=0;i<id.length;i++){
					id[i]=idsString1[i+1];
				}
			}else{ 
				id=new String[idsString1.length];
				id[0]=idsString1[0].split("_")[1];
				for (int i = 1; i < id.length; i++) {
					id[i]=idsString1[i];
				}
			}
		}else{//只有第一项被选
			id=new String[1];
			id[0]=idsString1[0].split("_")[1];
		}
		for (int i = 0; i < id.length; i++) {
			walletDetailBiz.deleteWalletDetail(id[i]);
		}
		return SUCCESS;
	}
	
	//充值
	public String addbalance() throws Exception{
		Map<String, Object>map=ActionContext.getContext().getSession();
		User user=(User) map.get("user");
		String userId=user.getUserId();
		Float sum1=(float)Integer.parseInt(sum);
		
		Float oldsum = walletBiz.getBalance(userId);
		float newsum=sum1+oldsum;
		
		Wallet wallet=walletBiz.getWallet(userId);
		wallet.setBalance(newsum);
		walletBiz.updateMoney(wallet);
		
		
		Wallet wallet2=walletBiz.getWallet(userId);
		Date date=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String strdate = sdf.format(date);
		Date day = sdf.parse(strdate);

		WalletDetail walletDetail=new WalletDetail((float)Integer.parseInt(sum), 1,day, wallet2.getUserId());
		walletDetail.setWallet(wallet2);
		walletDetailBiz.addWalletDetail(walletDetail);
		
		return null;
	}
	
	//提现
	public String withdrawals() throws Exception{
		Map<String, Object>map=ActionContext.getContext().getSession();
		HttpServletResponse response=ServletActionContext.getResponse();
		PrintWriter pw = response.getWriter();
		User user=(User) map.get("user");
		String userId=user.getUserId();
		Float sum1=(float)Integer.parseInt(sum);
		Float oldsum = walletBiz.getBalance(userId);
		if(sum1>oldsum){
			pw.write("fail");
		}else{
			float newsum=oldsum-sum1;
			Wallet wallet=walletBiz.getWallet(userId);
			wallet.setBalance(newsum);
			walletBiz.updateMoney(wallet);
			Wallet wallet2=walletBiz.getWallet(userId);
			Date date=new Date();
			WalletDetail walletDetail=new WalletDetail((float)Integer.parseInt(sum), 2,date, wallet.getUserId());
			walletDetail.setWallet(wallet2);
			walletDetailBiz.addWalletDetail(walletDetail);
			pw.write("success");
		}
		pw.flush();
		pw.close();
		return null;
	}
	
	//转账
	public String accounts() throws Exception{
		Map<String, Object>map=ActionContext.getContext().getSession();
		HttpServletResponse response=ServletActionContext.getResponse();
		PrintWriter pw = response.getWriter();
		User user=(User) map.get("user");
		String userId=user.getUserId();
		Float sum1=(float)Integer.parseInt(sum);
		Float oldsum = walletBiz.getBalance(userId);
		if(sum1>oldsum){
			pw.write("fail");
		}else{
			float newsum=oldsum-sum1;
			Wallet wallet=walletBiz.getWallet(userId);
			Wallet wallet2=walletBiz.getWallet(getUserId);
			Date date=new Date();
			WalletDetail walletDetail=new WalletDetail((float)Integer.parseInt(sum), 2,date, wallet.getUserId());
			WalletDetail walletDetail2=new WalletDetail((float)Integer.parseInt(sum), 1,date, wallet2.getUserId());
			
			wallet.setBalance(newsum);
			walletBiz.updateMoney(wallet);
			walletDetail.setWallet(wallet);
			walletDetailBiz.addWalletDetail(walletDetail);	
			
			wallet2.setBalance(walletBiz.getBalance(getUserId)+sum1);
			walletBiz.updateMoney(wallet2);
			walletDetail2.setWallet(wallet2);
			walletDetailBiz.addWalletDetail(walletDetail2);	
			pw.write("success");
		}
		pw.flush();
		pw.close();
		return null;

	}
	
	//付款
	public String pay() throws Exception{
		Map<String, Object>map=ActionContext.getContext().getSession();
		HttpServletResponse response=ServletActionContext.getResponse();
		PrintWriter pw = response.getWriter();
		User user=(User) map.get("user");
		String pwd=user.getPassword();
		String userId=user.getUserId();
		Float oldsum = walletBiz.getBalance(userId);
		if(password.equals(pwd)){
			if(sum2>oldsum){
				pw.write("fail1");
			}else{
			float newsum=oldsum-sum2;
			Wallet wallet=walletBiz.getWallet(userId);
			wallet.setBalance(newsum);
			walletBiz.updateMoney(wallet);
			Wallet wallet2=walletBiz.getWallet(userId);
			Date date=new Date();
			WalletDetail walletDetail=new WalletDetail(sum2, 2,date, wallet.getUserId());
			walletDetail.setWallet(wallet2);
			walletDetailBiz.addWalletDetail(walletDetail);
			pw.write("success");}
		}else{
			pw.write("fail2");
		}
		pw.flush();
		pw.close();
		return null;
	}
	public WalletBiz getWalletBiz() {
		return walletBiz;
	}


	public void setWalletBiz(WalletBiz walletBiz) {
		this.walletBiz = walletBiz;
	}


	public WalletDetailBiz getWalletDetailBiz() {
		return walletDetailBiz;
	}


	public void setWalletDetailBiz(WalletDetailBiz walletDetailBiz) {
		this.walletDetailBiz = walletDetailBiz;
	}

public static void main(String[] args) throws Exception {
	new WalletAction().deleteWallet();
}
	
}
